"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../../const/index.js");
const utils = require("utils");
function getCookieFromHeader(headers) {
  if (headers && headers["set-cookie"]) {
    let cookies = headers["set-cookie"];
    if (Array.isArray(cookies) && cookies.length > 0) {
      let cookieMap = {};
      cookies.forEach((e) => {
        if (typeof e === "string") {
          let splitArr = e.split(/[;；]/);
          splitArr.forEach((item) => {
            let subArr = item.split("=");
            if (subArr.length === 2) {
              cookieMap[subArr[0]] = subArr[1];
            }
          });
        }
      });
      return cookieMap[index.JSESSIONIDKey];
    }
  }
  return void 0;
}
function getNumberValue(o) {
  if (utils.util.nilValue(o)) {
    return null;
  }
  if (typeof o === "number") {
    return o;
  }
  if (typeof o === "string") {
    let tmp = o.trim();
    if (tmp.length === 0) {
      return null;
    }
    let num = Number(tmp);
    if (Number.isNaN(num)) {
      return null;
    }
    return num;
  }
  return null;
}
function number2String(num) {
  if (Number.isInteger(num)) {
    return String(num);
  }
  return num.toFixed(2);
}
function formatterData(origin) {
  var _a, _b, _c, _d;
  let expireDate = "";
  let next = origin == null ? void 0 : origin.nextCardLifeList;
  if (Array.isArray(next) && next.length > 0) {
    expireDate = next[0].nextExpireDateStr ?? "";
  } else {
    expireDate = ((_a = origin == null ? void 0 : origin.entity) == null ? void 0 : _a.expireDate) ?? "";
  }
  let total = "";
  let left = "";
  let totalNumber = getNumberValue((_b = origin == null ? void 0 : origin.entity) == null ? void 0 : _b.flowSize);
  if (!utils.util.nilValue(totalNumber)) {
    total = number2String(totalNumber / 1024);
    let useNumber = getNumberValue((_c = origin == null ? void 0 : origin.entity) == null ? void 0 : _c.totalBytesCnt);
    if (!utils.util.nilValue(useNumber)) {
      left = number2String((totalNumber - useNumber) / 1024);
    }
  }
  return {
    expireDate,
    total,
    left,
    status: ((_d = origin == null ? void 0 : origin.entity) == null ? void 0 : _d.statusStr) ?? "未知状态"
  };
}
exports.formatterData = formatterData;
exports.getCookieFromHeader = getCookieFromHeader;
